package com.google.project.client.HumanResources;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.project.client.Service.SpecializeService;
import com.google.project.client.Service.SpecializeServiceAsync;
import com.google.project.shared.SpecializeDTO;
import com.google.gwt.user.client.ui.Label;

public class HMAddSkill extends Composite {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField Button btnSave;
	@UiField Button btnCancel;
	@UiField TextBox txtSpecializeName;
	@UiField TextArea txtDescription;
	@UiField Label lblTitle;
	private SpecializeDTO currentSpec = new SpecializeDTO();
	private final SpecializeServiceAsync specService = GWT.create(SpecializeService.class);

	interface Binder extends UiBinder<Widget, HMAddSkill> {
	}

	public HMAddSkill() {
		initWidget(binder.createAndBindUi(this));
	}

	public HMAddSkill(String specializeId) {
		this();
		getSpecializeInfo(specializeId);
	}

	private void getSpecializeInfo(String specializeId) {
		specService.getSpecializeById(specializeId,
				new AsyncCallback<SpecializeDTO>() {
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Xảy ra lỗi trong quá trình cập nhật!");
					}

					@Override
					public void onSuccess(SpecializeDTO result) {
						currentSpec = result;
						displayInfo();
					}

				});
	}

	private void displayInfo() {
		txtDescription.setText(currentSpec.getDescription());
		txtSpecializeName.setText(currentSpec.getSpecializeName());
		lblTitle.setText("CẬP NHẬT THÔNG TIN CHUYÊN MÔN");
	}
	
	@UiHandler("btnSave")
	void onBtnSaveClick(ClickEvent event) {
		getFormValues();
		if (currentSpec.getId() == null) {
			specService.addSpecialize(currentSpec,
					new AsyncCallback<SpecializeDTO>() {

						@Override
						public void onSuccess(SpecializeDTO result) {
							Window.alert("Thêm mới thành công!");
							HMEntrypoint.get().HMSkillList();
						}

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Có lỗi xảy ra!");

						}
					});
		} else {
			specService.updateSpecialize(currentSpec,
					new AsyncCallback<Void>() {

						@Override
						public void onSuccess(Void result) {
							Window.alert("Cập nhật thành công!");
							HMEntrypoint.get().HMSkillList();
						}

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Có lỗi xảy ra!");
						}
					});

		}
	}

	private void getFormValues() {
		currentSpec.setSpecializeName(txtSpecializeName.getText());
		currentSpec.setDescription(txtDescription.getText());
	}

	@UiHandler("btnCancel")
	void onBtnCancelClick(ClickEvent event) {
		HMEntrypoint.get().HMSkillList();
	}
}
